package com.cainiao.stock.service.Impl;

import com.cainiao.stock.cache.Cache;
import com.cainiao.stock.cache.impl.RedisCache;
import com.cainiao.stock.config.PasswordEncoderConfiguration;
import com.cainiao.stock.constant.Constants;
import com.cainiao.stock.dto.LoginDTO;
import com.cainiao.stock.entity.SysUser;
import com.cainiao.stock.enums.CodeEnum;
import com.cainiao.stock.exception.Exceptions;
import com.cainiao.stock.mapper.SysUserMapper;
import com.cainiao.stock.service.UserService;
import com.cainiao.stock.util.IdWorkers;
import com.cainiao.stock.util.RedisKeyUtil;
import com.cainiao.stock.vo.CaptchaVO;
import com.cainiao.stock.vo.LoginVO;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author zby
 * @created 2024-06-24 20:18
 * @description 用户业务实现类
 */
@Service
public class UserServiceImpl implements UserService {


    private final Cache cache;
    private final IdWorkers idWorkers;
    private final RedisCache redisCache;
    private final SysUserMapper sysUserMapper;
    private final PasswordEncoderConfiguration passwordEncoderConfiguration;

    public UserServiceImpl(Cache cache, IdWorkers idWorkers, RedisCache redisCache, SysUserMapper sysUserMapper, PasswordEncoderConfiguration passwordEncoderConfiguration) {
        this.cache = cache;
        this.idWorkers = idWorkers;
        this.redisCache = redisCache;
        this.sysUserMapper = sysUserMapper;
        this.passwordEncoderConfiguration = passwordEncoderConfiguration;
    }


    @Override
    public CaptchaVO captcha() {

        // 1. 使用工具类生成一个随机的4位验证码
        String captcha = RandomStringUtils.randomNumeric(4);
        // 2. 将验证码存入缓存
        // 生成key的后缀
        String keySuffix = idWorkers.nextStrId();
        // 生成key
        String redisKey = RedisKeyUtil.generateKey(Constants.CAPTCHA_PREFIX, keySuffix);
        // 存入缓存，并且设置过期时间
        cache.set(redisKey, captcha, Constants.CATPCHA_EXPIRE_TIME);
        // 3. 数据组装返回
        return   CaptchaVO.builder()
                        .code(captcha)
                        .rkey(redisKey)
                        .build();
    }

    @Override
    public LoginVO selectByName(LoginDTO loginDTO) {
        if(ObjectUtils.isEmpty(loginDTO) ||
                ObjectUtils.isEmpty(loginDTO.getUsername()) ||
                ObjectUtils.isEmpty(loginDTO.getPassword())||
                ObjectUtils.isEmpty(loginDTO.getCode())||
                ObjectUtils.isEmpty(loginDTO.getRkey())){
            Exceptions.cast(CodeEnum.DATA_ERROR);
        }
        String rkey= loginDTO.getRkey();
        if(!StringUtils.equals(redisCache.get(rkey),loginDTO.getCode())){
            Exceptions.cast(CodeEnum.SYSTEM_VERIFY_CODE_ERROR);
        }
        redisCache.remove(rkey);
        SysUser user=sysUserMapper.selectByName(loginDTO.getUsername(),Constants.SYS_USER_STATUS_ON,Constants.SYS_USER_UNDELECT);

        if(ObjectUtils.isEmpty(user)||
        !passwordEncoderConfiguration.passwordEncoder().matches(loginDTO.getPassword(),
                user.getPassword())){
            Exceptions.cast(CodeEnum.SYSTEM_PASSWORD_ERROR);

        }

        return LoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .phone(user.getPhone())
                .nickName(user.getNickName())
                .build();
    }
}
